import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
import { Platform, IonicPage, NavController, NavParams, ToastController,} from 'ionic-angular';
import { Https } from '../../app/http.service'
import { InAppBrowser ,InAppBrowserOptions} from '@ionic-native/in-app-browser';

// import { InAppBrowser } from '@ionic-native/in-app-browser';
/**
 * Generated class for the KataotaoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()

@Component({
  selector: 'page-kataotao',
  templateUrl: 'kataotao.html',
})
export class KataotaoPage {
	@ViewChild('frame')
	fram: ElementRef;
	hisy = 1;
	Url: string = new Https().Url;
	iframe:any
	dom:any
	iframeUrl = "http://www.s.cardtaotao.com/wap/"; 
	constructor(public el: ElementRef,private iab: InAppBrowser,
		public navCtrl: NavController, public toastCtrl: ToastController, 
		public navParams: NavParams, private platform: Platform, 
		private elementRef: ElementRef) {
		let thin = this; 
		console.log(this.fram)
		//如果是安卓
		if (!navigator.userAgent.match(/iPad|iPhone/i)) {

		}else{
			this.navCtrl.pop();
			const options: InAppBrowserOptions = {
				location: 'no',
				clearcache: 'yes',
				toolbarcolor: '#ff0000',
				closebuttoncaption: "关闭",
				closebuttoncolor: '#ffffff',
				toolbar: 'yes',
				toolbarposition: 'top',
				transitionstyle: 'fliphorizontal',
				// hidespinner:'yes',
				navigationbuttoncolor: '#ffffff',
				disallowoverscroll: 'yes',
				hidespinner: 'yes',


			}
			
			const browser = this.iab.create(this.iframeUrl, '_blank', options);
		}
		
		
		
	}
	ngAfterViewInit() {
		this.iframe = this.fram.nativeElement
		this.iosIframeWidthBug()
	}
	
	back(frame){
		let thin = this;
		if (thin.hisy == 1){
			let his = frame['contentWindow'].history
			let hisLen = his.length-1;
			if (frame['contentWindow'].location.href != this.iframeUrl) {
				his.go(-1);
			} else {
				thin.navCtrl.pop();
			}
		}else{
			thin.navCtrl.pop();
		}
		
		//
	}
	close(){
		this.navCtrl.pop();
	}
	fresh(frame){
		let thin = this;
		// thin.iframe['contentWindow'].location.href = thin.iframe.src;
		frame.contentWindow.location.reload()
		console.dir(frame)
		// console.dir(thin.iframe.parentElement.)
	}
	ionViewDidLoad() {
		//back
		let thin = this;
		this.platform.registerBackButtonAction(() => {
			// this.back()
			console.log(11122221)
			let his = thin.iframe.contentWindow.history
			let hisLen = his.length - 1;
			if (thin.iframe.contentWindow.location.href != thin.iframeUrl) {
				his.go(-1);
			} else {
				thin.navCtrl.pop();
			}
		}, 101);
	}
	ionViewWillLeave(){
		//back
		this.platform.registerBackButtonAction(() => {
			// this.back()
				this.navCtrl.pop();
		}, 101);
	}

	/*修复ios iframe width bug*/
	iosIframeWidthBug() {
	//不是 iphone ipad就不执行了
	if (!navigator.userAgent.match(/iPad|iPhone/i))
		return false;
	//获取子iframe
		// iframe_box.style.width = 100 + '%';
		// iframe_box.style.overflowX = 'hidden';
		// iframe_box.style.overflowY = 'scroll';
		// iframe_box.style.webkitOverflowScrolling = 'touch';
		// ifram.setAttribute('scrolling', 'no');
		// iframe_box.appendChild(ifram)

}

}
